<?php

/**
* @author Liu Shan Shui <me@lx.sg>
* @link http://code.google.com/p/curlx/
* @license http://lx.sg/mit09
*/

/*

Examples:
- Simplified GET request (to fetch a web page, similar to file_get_contents())
$curlx = new Curlx;
$web_page = $curlx->fetch('http://example.com/');
 
- Simplified POST request
$curlx = new Curlx;
$web_page = $curlx->fetch('http://example.com/', array('username' => 'User', 'password' => 'pa55word'));

- Site login
// Imagine you're using a social networking site on example.com, 
// and you want your PHP script to log in and update your profile for you.
$curlx = new Curlx;
// This file will be used whenever there's cookie data to be stored or sent to the server.
$curlx->cookie('my_cookie.text');

// Send a POST request containing your username and password to the login page
$response = $curlx->fetch('http://example.com/login', array('username' => 'User', 'password' => 'pa55word'));

// Randomly pick a mood for your new profile status
$moods = array('happy', 'sad', 'angry');
$current_mood = $moods[mt_rand(0, count($moods) - 1)];
$status = "I am $current_mood today!";

// Do a status update on your example.com profile
$response = $curlx->fetch('http://example.com/profile', array('status' => $status));

*/

class Curlx {
	private $handle;
	
	public function __construct($blankState = false) {
		$this->handle = curl_init();
		$this->options = array();
		if (!$blankState) {
			$this->returnResponse(true);
			$this->followRedirects(true);
			$this->userAgent('Mozilla/5.0 (Curlx - http://code.google.com/p/curlx/)');
			$this->timeout(20);
		}
	}
	
	public function fetch($url, $post = null) {
		$this->URL($url);
		$this->POST($post);
		$response = $this->execute();
		$this->URL(null);
		$this->POST(null);
		return $response;
	}
	
	public function set($option, $value) {
		curl_setopt($this->handle, $option, $value);
	}
	
	public function returnResponse($bool) {
		$this->set(CURLOPT_RETURNTRANSFER, $bool);
	}
	
	public function followRedirects($bool) {
		$this->set(CURLOPT_FOLLOWLOCATION, $bool);
	}
	
	public function userAgent($agent) {
		$this->set(CURLOPT_USERAGENT, $agent);
	}
	
	public function ignoreSSL($bool) {
		if ($bool) {
			$this->set(CURLOPT_SSL_VERIFYPEER, false);
			$this->set(CURLOPT_SSL_VERIFYHOST, false);
		} else {
			$this->set(CURLOPT_SSL_VERIFYPEER, true);
			$this->set(CURLOPT_SSL_VERIFYHOST, 2);
		}
	}
	
	public function URL($url) {
		$this->set(CURLOPT_URL, $url);
	}
	
	public function timeout($seconds) {
		$this->set(CURLOPT_TIMEOUT, $seconds);
	}
	
	public function POST($data) {
		if ($data) {
			$this->set(CURLOPT_POST, true);
			
			/*
			When an array is provided, cURL assumes that you're doing a 
			file upload and sends a Content-Type of multipart/form-data.
			Thus, to avoid this behaviour, we convert the data array into
			a string when a file upload is not involved.
			For more information, refer to:
			 - http://sg.php.net/manual/en/function.curl-setopt.php
			 - http://curl.haxx.se/mail/curlphp-2001-11/0001.html
			*/
			if (is_array($data)) {
				$file_upload = false;
				foreach ($data as $name => $value) {
					if (strpos($value, '@') !== 0) {
						$nv_data[] = urlencode($name) . '=' . urlencode($value);
					} else {
						$file_upload = true;
						break;
					}
				}
				if (!$file_upload) $data = implode('&', $nv_data);
			}
			
			$this->set(CURLOPT_POSTFIELDS, $data);
		} else {
			$this->set(CURLOPT_POST, false);
		}
	}
	
	public function cookie($path) {
		$this->set(CURLOPT_COOKIEJAR, $path);
		$this->set(CURLOPT_COOKIEFILE, $path);
	}
	
	public function referer($referrer) {
		$this->set(CURLOPT_REFERER, $referrer);
	}
	
	public function execute() {
		return curl_exec($this->handle);
	}
	
	public function info($option = null) {
		return $option ? curl_getinfo($this->handle, $option) : curl_getinfo($this->handle);
	}
	
	public function error() {
		return array('error' => curl_error($this->handle), 'errno' => curl_errno($this->handle));
	}
	public function __destruct() {
		curl_close($this->handle);
	}		
}

?>